import Vue from 'vue'

// 1.1 导入路由的包
import VueRouter from 'vue-router'
// 1.2 安装路由
Vue.use(VueRouter)

//导入vuex
import Vuex from 'vuex'
Vue.use(Vuex);

import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root = 'http://www.liulongbin.top:3005';
// 全局设置 post 时候表单数据格式组织形式   application/x-www-form-urlencoded
Vue.http.options.emulateJSON = true;

// 导入 MUI 的样式
import './lib/mui/css/mui.min.css'
// 导入扩展图标样式
import './lib/mui/css/icons-extra.css'

// import { Header,Swipe, SwipeItem } from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'


import App from './App.vue'

import router from './router.js'

import VuePreview from 'vue2-preview'
Vue.use(VuePreview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: {
    top: 0,
    bottom: 0
  },
  captionEl: false,
  fullscreenEl: false,
  shareEl: false,
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: false
})

//自定义一个时间过滤器
import moment from 'moment'
Vue.filter('dateformat', function (datastr, pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(datastr).format(pattern);
})

var car = JSON.parse(localStorage.getItem('car') || '[]')

const store = new Vuex.Store({
  state: {
    car: car,
    //{id:"",count:"",price:"",selected:true}
  },
  mutations: {
    addToCar(state, goodsinfo) {
      //加入购物车
      var flag = false // 假设 在购物车中，没有找到对应的商品
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = item.count + goodsinfo.count;
          flag = true;
          return true
        }
      })
      if (!flag) {
        state.car.push(goodsinfo)
      }

      localStorage.setItem('car', JSON.stringify(state.car))
    },
    upgoodsCount(state, goodsinfo) {
      //在购物车页面更新商品数量
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = parseInt(goodsinfo.count);
          return true;
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    removeGoods(state, id) {
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(i, 1);
        }
        return true
      })
      // 将删除完毕后的，最新的购物车数据，同步到 本地存储中
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsSelected(state, goodsinfo) {
      //更新该商品是否选中
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.selected = goodsinfo.selected
          return true;
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters: {
    getAllCount(state) {
      var c = 0;
      state.car.forEach(item => {
        c += item.count;
      })
      return c;
    },
    getAllId(state) {
      var ids = [];
      state.car.forEach(item => {
        ids.push(item.id);
      })
      return ids
    },
    getGoodsCount(state) {
      var o = {}
      state.car.forEach(item => {
        o[item.id] = item.count;
      })
      return o;
    },
    getGoodsSelected(state) {
      var o = {}
      state.car.forEach(item => {
        o[item.id] = item.selected;
      })
      return o;
    },
    getAlltotal(state) {
      var total = 0;
      state.car.forEach(item => {
        if (item.selected) {
          total += item.price * item.count
        }
      })
      return total
    }
  }
})


var vm = new Vue({
  el: '#app',
  data: {
    msg: '123'
  },
  render: c => c(App),
  router,
  store
})